package com.abe.app;

import org.xutils.x;

import com.abe.clickeffect.ClickEffectTool;
import com.abe.utils.NormalUtils;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

/**
 * @see 界面适配框架
 * @see Xutils注解注册
 * */
public class AdaptiveActivity extends Activity {

	protected static float scale = 0;
	private static final float UI_STANDARD_width = 1280;

	@Override
	public void setContentView(int layoutResID) {
		View view = View.inflate(this, layoutResID, null);
		this.setContentView(view);
	}

	@Override
	public void setContentView(View view) {
		if (scale == 0) {
			initScreenScale();
		}
		if (scale != 1) {
			RelayoutTool.relayoutViewHierarchy(view, scale);
			ClickEffectTool.unifiedAddClickEffect(view);
		}
		super.setContentView(view);
	}

	@Override
	public void setContentView(View view, LayoutParams params) {
		if (scale == 0) {
			initScreenScale();
		}
		RelayoutTool.relayoutViewHierarchy(view, scale);
		RelayoutTool.scaleLayoutParams(params, scale);
		ClickEffectTool.unifiedAddClickEffect(view);
		super.setContentView(view, params);
	}

	public void initScreenScale() {
		DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
		float width = displayMetrics.widthPixels;
		scale = width / UI_STANDARD_width;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		HideVirtualMenu();
		ManagerAPP.getInstance().addActivity(this);
		x.view().inject(this);
	}

	protected void HideVirtualMenu() {
		boolean haveVirtualMenu = NormalUtils
				.checkDeviceHasNavigationBar(getApplicationContext());
		if (haveVirtualMenu) {
			NormalUtils.hideBottomUIMenu(this);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ManagerAPP.getInstance().remove(this);
	}

}
